﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Eneta.Portal.Common.Dto;
using Eneta.Portal.Common.Definitions;
using Eneta.Portal.Common.Helpers;

namespace Eneta.Portal.Common.Repository
{
    public class BookRepository : BaseRepository
    {
        private const string PagesListUrl = "/oppimine/raamatud/Lehed/";
        private const string BooksWebUrl = "/oppimine/raamatud/";

        public BookRepository(SPWeb web)
            : base(web)
        {

        }

        public bool BookExists(string bookName)
        {
            var exists = false;

            var sec = new SPSecurity.CodeToRunElevated(delegate
            {
                using (var site = new SPSite(SiteId))
                using (var web = site.OpenWeb(BooksWebUrl))
                {
                    var list = web.GetList(PagesListUrl);
                    var queryString = 
                            string.Format(
                                "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>{0}</Value></Eq></Where>",
                                bookName
                            );
                    var items = ExecuteQuery(list, queryString);
                    if (items == null)
                        exists = false;

                    if (items.Count > 0)
                        exists = true;                    
                }
            });
            SPSecurity.RunWithElevatedPrivileges(sec);

            return exists;
        }

        public PageLayout GetBookLayout(SPWeb web)
        {
            var publishingWeb = PublishingWeb.GetPublishingWeb(web);
            var ct = web.AvailableContentTypes["Raamat"];
            if (ct == null)
            {
                foreach (SPContentType ct2 in web.AvailableContentTypes)
                {
                    if (ct2.Name == "Raamat")
                    {
                        ct = ct2;
                        break;
                    }
                }
            }
            if (ct == null)
            {

                foreach (var layout in publishingWeb.GetAvailablePageLayouts())
                {
                    if (layout.Name.ToLower() == "eneta.learning.bookdetail.layout.aspx")
                    {
                        return layout;
                    }
                }
            }
            else
            {
                var publishingLayout = publishingWeb.GetAvailablePageLayouts(ct.Id);
                return publishingLayout[0];
            }

            return null;
        }

        public IList<LiteratureDto> GetUserBooks(int userId)
        {
            var results = new List<LiteratureDto>();

            var queryString = "<Where><Eq><FieldRef Name='RealAuthor' LookupId='True' /><Value Type='User'>{0}</Value></Eq></Where>";
            queryString += "<Eq><FieldRef Name='ContentType' /><Value Type='Text'>{1}</Value></Eq></And></Where>";
            queryString = string.Format(queryString, userId, "Raamat");

            var sec = new SPSecurity.CodeToRunElevated(delegate
            {
                using (var site = new SPSite(SiteId))
                using (var web = site.OpenWeb(Webs.Books))
                {
                    var list = web.Lists[Lists.Pages];
                    var items = ExecuteQuery(list, queryString);

                    foreach (SPListItem item in items)
                    {
                        results.Add(new LiteratureDto(web, item));
                    }
                }
            });
            SPSecurity.RunWithElevatedPrivileges(sec);

            return results;
        }

    }
}
